<?php


require_once( "_inc.php" );
$categoryId = !empty( $_GET['cId'] ) ? intval( $_GET['cId'] ) : "";
$backUrl = !empty( $_GET['back'] ) ? trim( $_GET['back'] ) : "";
if ( empty( $categoryId ) )
{
	header( "location: category_list.php" );
	exit( );
}
if ( !empty( $_GET['btnSubmit'] ) )
{
	util::auth( );
	$name = !empty( $_GET['name'] ) ? trim( $_GET['name'] ) : "";
	$parentId = !empty( $_GET['parentId'] ) ? trim( $_GET['parentId'] ) : "0";
	$englishName = !empty( $_GET['englishName'] ) ? trim( $_GET['englishName'] ) : "";
	$autoList = !empty( $_GET['autoList'] ) ? trim( $_GET['autoList'] ) : "0";
	$order = !empty( $_GET['order'] ) ? trim( $_GET['order'] ) : "";
	$keywords = !empty( $_GET['keywords'] ) ? trim( $_GET['keywords'] ) : "";
	$description = !empty( $_GET['description'] ) ? trim( $_GET['description'] ) : "";
	$url = !empty( $_GET['url'] ) ? trim( $_GET['url'] ) : "";
	if ( empty( $name ) )
	{
		$tpl->assign( "msg", "缺少参数。" );
		$tpl->display( "_msg.tpl" );
		exit( );
	}
	if ( !empty( $englishName ) )
	{
		$id = $db->fetchone( "SELECT f_id FROM ".$gDb['prefix'].( "categories WHERE (f_englishName='".$englishName."' AND f_id<>'{$categoryId}')" ) );
		if ( $id )
		{
			$tpl->assign( "msg", "英文名称“ ".$englishName." ”已经存在。请返回修改。" );
			$tpl->display( "_msg.tpl" );
			exit( );
		}
	}
	$sql = "UPDATE ".$gDb['prefix'].( "categories SET f_name='".$name."',f_parentId='{$parentId}', f_order='{$order}', f_autoList='{$autoList}', f_englishName='{$englishName}', f_keywords='{$keywords}', f_description='{$description}', f_url='{$url}' WHERE (f_id='{$categoryId}')" );
	$db->query( $sql );
	$sql = "UPDATE ".$gDb['prefix'].( "categories SET f_haveChild='1' WHERE (f_id='".$parentId."')" );
	$db->query( $sql );
	$tpl->assign( "msg", "修改成功。" );
	$tpl->assign( "refreshUrl", "sort.php" );
	$tpl->assign( "backUrl", base64_decode( $backUrl ) );
	$tpl->display( "_msg.tpl" );
	exit( );
}
$tpl->assign( "categoryId", $categoryId );
$tpl->assign( "backUrl", $backUrl );
$tpl->display( );
?>
